import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/in_out_record_entity.dart';

class OutboundRecordsController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 开始时间
  String startTime = '';

  /// 结束时间
  String endTime = '';

  /// 产品id
  String productId = '';

  /// 仓库id
  String warehouseId = '';

  /// 获取产品出库记录数据
  Future<List<InOutRecordEntity>> getProductRecordsListData(
    int pageNum,
    int pageSize,
  ) async {
    List<InOutRecordEntity> recordList = [];

    final res = await HttpUtil().generateGetRequest<List<InOutRecordEntity>>(
      queryParameters: {
        'productId': productId,
        'warehouseId': warehouseId,
        'type': 0,
        'startDate': startTime,
        'endDate': endTime,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
      Interface.getRecordList,
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        recordList = result.data!;
      }
    });
    return recordList;
  }

  @override
  void onInit() {
    super.onInit();
    final Map<String, dynamic> arguments = Get.arguments as Map<String, dynamic>;
    try {
      if (arguments['productId'] != null && arguments['productId'] is String) {
        productId = arguments['productId'] as String;
      }
      warehouseId = arguments['warehouseId'] as String;
    } catch (e) {
      e.printError();
    }
  }

  // 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
